﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Data;
using System.Windows.Input;
using MediaMover.MVVM.Commands;
using MediaMover.Ninject;
using Ninject;

namespace MediaMover.MVVM.ViewModels
{
    public class MainWindowViewModel
    {
        private IKernel _kernel = new StandardKernel(new MainModule());
        private ObservableCollection<WorkspaceViewModel> _workspaces;
        public MainWindowViewModel()
        {
            Workspaces.Add(new MyShowsViewModel(_kernel));
            Workspaces.Add(new ScanViewModel(_kernel));

        }
        #region Commands
        private ICommand _optionCommand;
        public ICommand OptionCommand
        {
            get
            {
                if (_optionCommand == null)
                {
                    _optionCommand = new RelayCommand(param => ShowOptions(), x => true);
                }
                return _optionCommand;
            }
            set { _optionCommand = value; }
        }

        private void ShowOptions()
        {
            var options = Workspaces.FirstOrDefault(vm => vm is OptionsViewModel) as OptionsViewModel;
            if (options == null)
            {
                options = new OptionsViewModel(_kernel);
                _workspaces.Add(options);
            }
            SetActiveWorkspace(options);
        } 
        #endregion
         
        #region Workspaces

        /// <summary>
        /// Returns the collection of available workspaces to display.
        /// A 'workspace' is a ViewModel that can request to be closed.
        /// </summary>
        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                if (_workspaces == null)
                {
                    _workspaces = new ObservableCollection<WorkspaceViewModel>();
                    _workspaces.CollectionChanged += OnWorkspacesChanged;
                }
                return _workspaces;
            }
        }

        void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.NewItems)
                    workspace.RequestClose += OnWorkspaceRequestClose;

            if (e.OldItems != null && e.OldItems.Count != 0)
                foreach (WorkspaceViewModel workspace in e.OldItems)
                    workspace.RequestClose -= OnWorkspaceRequestClose;
        }

        void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            WorkspaceViewModel workspace = sender as WorkspaceViewModel;
            if (workspace != null && workspace.GetType() != typeof(MyShowsViewModel) && workspace.GetType() != typeof(ScanViewModel))
            {
                workspace.Dispose();
                Workspaces.Remove(workspace);
            }
        }
        
        private void SetActiveWorkspace(WorkspaceViewModel workspace)
        {
            Debug.Assert(Workspaces.Contains(workspace));
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(Workspaces);
            if (collectionView != null)
                collectionView.MoveCurrentTo(workspace);
        }
        #endregion // Workspaces
    }
}
